package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class CommonExceptionHandler {

    // 请求出异常之后, 抛出LogicException 统一交给这个方法处理displayExp
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object displayExp(Exception e, HttpServletResponse resp, Model model) throws IOException {
        return new AjaxResult(false, e.getMessage());
    }


    // 请求出异常之后, RuntimeException 统一交给这个方法处理runTimeExp
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp, Model model) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        return new AjaxResult(false, e.getMessage());
    }
}
